<template>
  <div>
    <!-- 送达通知书打印 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhNoticePrinting"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <!-- <multiuse-button size="small" type="primary" :click="exportHandler">导 出</multiuse-button> -->
        <multiuse-button size="small" type="primary" @click="addHandle()" :disabled="isOpen">打印</multiuse-button>
        <AjaxButton type="primary" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerclose" />
    </BaseDialog>

    <!-- 打印预拣单 -->
    <div v-if="isprint && appId == 'beijing'">
      <BasePrint ref="printPick" :data="printData" template="templateCertificate" />
    </div>

    <div v-if="isprint && appId == 'arcfox'">
      <BasePrint ref="printPick" :data="printData" template="templateCertificateAF" />
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { getAppId } from '@/assets/js/auth';

import { getList, getprinting, getFactoryWarehouseList, gettmFiAccountType, exporturl } from '@/api/omSale/certificate/NoticePrinting/index.js';
import ownerSeacher from './ownerSeacher';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'NoticePrinting',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: { xhrAbort: true },
      appId: getAppId(),
      formData: {},
      visible: false,
      issearch: false,
      isreject: false,
      isprint: false,
      printData: {},
      selectList: [],
      isOpen: true,
      dealerCode: ''
    };
  },
  components: {
    ownerSeacher
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getFactoryWarehouseList();
    this.gettmFiAccountType();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '三方票号',
          fieldName: 'billNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountTypeCode',
          placeholder: '请选择',
          itemList: this.createDictList('6005')
        },
        {
          type: 'RANGE_DATE',
          label: '绑定日期',
          fieldName: 'startCheckDate|endCheckDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '打印状态',
          fieldName: 'printStatus',
          placeholder: '请选择',
          itemList: [
            { text: '已打印', value: 1 },
            { text: '未打印', value: 0 }
          ]
        },

        {
          type: 'SELECT',
          label: '下线工厂',
          fieldName: 'factoryId',
          placeholder: '请选择',
          itemList: []
        },

        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'beginOutDate|endOutDate',
          placeholder: '请输入',
          initialValue: [
          ],
          rules: [{ required: false, message: '请选择日期', trigger: 'change' }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '三方号码',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
         { title: '车型',
           dataIndex: 'modelCode',
            sorter: true ,
             filter: true,
          filterType: 'input'},
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '合格证地址',
          dataIndex: 'certificateMailAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
       {
          title: '监管人',
          dataIndex: 'supervisor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '资金类型代码',
          dataIndex: 'accountTypeCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

       
        {
          title: '下线工厂',
          dataIndex: 'factoryName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '打印状态',
          dataIndex: 'printStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '打印次数',
          dataIndex: 'printCount',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.printCount ? p.row.printCount : 0}</span>;
          }
        },
        {
          title: '最近打印时间',
          dataIndex: 'printDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        
        {
          title: '监管电话',
          dataIndex: 'supervisorPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 导出
    async exportHandler() {
      let json = Object.assign({}, { CODE: 'CODE_00000257' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        this.selectList = row;
        this.isOpen = false;
      } else {
        this.selectList = [];
        this.isOpen = true;
      }
    },
    seacherFun() {
      this.issearch = true;
    },
    async addHandle(type) {
      let arr = [];
      this.selectList.map(x => {
        arr.push(x.doId);
      });
      let res = await getprinting({ ids: arr });
      if (res.data) {
        this.isprint = true;
        this.printData = res.data;
        this.printData.companyName = this.appId == 'arcfox' ? '北京蓝谷极狐汽车科技有限公司' : res.data.companyName;
        this.printData.bYear = moment(this.printData.billDate).format('YYYY');
        this.printData.bMouth = moment(this.printData.billDate).format('MM');
        this.printData.bDay = moment(this.printData.billDate).format('DD');
        this.printData.eYear = moment(this.printData.expireDate).format('YYYY');
        this.printData.eMouth = moment(this.printData.expireDate).format('MM');
        this.printData.eDay = moment(this.printData.expireDate).format('DD');
        this.printData.dYear = moment().format('YYYY');
        this.printData.dMouth = moment().format('MM');
        this.printData.dDay = moment().format('DD');
        this.printData.vehicleVOs.push({ engineNo: '金额合计', oemDirectivePrice: res.data.sumPrice, productName: '', vin: '' });
        this.$nextTick(() => {
          this.$refs.printPick.EXCUTE_PRINT();
        });
      }
    },
    changeHandle(params) {
      this.isOpen = true;
      this.fetchParams.dealerCode = this.dealerCode;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    ownerclose(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    resetChange() {
      this.isOpen = true;
      this.dealerCode = '';
      this.fetchParams.dealerCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    async getFactoryWarehouseList() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'factoryId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    // 导出
    async exportHandle() {
      let json = Object.assign({}, { CODE: 'CODE_00000244' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    // 加载资金类型
    async gettmFiAccountType(powerType) {
      let res = (await gettmFiAccountType({ fundsType: 0, accountType: 60051001 })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountTypeCode').itemList = res;
    }
  }
};
</script>
